package  net.libra.core
{
	import com.cw.utils.AsyncUtils;
	import com.cw.utils.StringUtils;
	import net.libra.core.interfaces.IController;
	import net.libra.core.interfaces.INotifier;
	
	import flash.utils.Dictionary;
	
	import net.libra.notifier.Notification;

	/**
	 * 事件转发器
	 * @author elvisjiang
	 * @data 2012-6-11
	 */	
	public class Facade implements INotifier
	{
		private static var instance:Facade;
		
		public static function getInstance () : Facade
		{
			if (instance == null)
			{
				instance = new Facade ();
			}
			return instance;
		}
		public function Facade()
		{
			controllerMap = new Dictionary(true);
			asyncUtilsMap = new Dictionary(true);
			if (instance != null) throw new Error ( "Singleton Error" );
		}
		/**
		 * controllerMap[ name ][0]是存着的IController对象
		 * controllerMap[ name ][1]是存折IController对象关心的消息
		 */		
		public function registerContoller(name:String,controllerRef:Class):void
		{
			if ( controllerMap[ name ] == null ) {
				var obj:IController = new controllerRef() as IController;
				obj.initialize();
				controllerMap[ name ] = new Array();
				controllerMap[ name ][0] = obj;
				var commandArray:Array = obj.listNotificationInterests();
				controllerMap[ name ][1] = commandArray;
			}
		}
		public function getContoller(name:String):IController
		{
			var obj:IController = controllerMap[name][0];
			return obj;
		}
		public function removeContoller( name : String ) : void
		{
			if ( controllerMap[name] )
			{
				controllerMap[name].length = 0;
				delete controllerMap[name];
			}
		}
		/**
		 * 向所有的IController发消息
		 * 该函数会遍历所有的已注册的IController，
		 * 如果发的消息在那个IController里面就执行handleNotification，
		 * 没有则进入下一个。
		 * @param notificationName
		 * @param body
		 * @param controllerName 可以指定哪个Controller 用于提高性能
		 * @param type
		 * 
		 */		
		public function sendNotification(notificationName:String,body:Object = null,controllerName:String="",type:String = null):void
		{
			if(controllerName != "")
			{
				if(controllerMap[controllerName] == null){
					throw new Error("controller还没有注册，你就用他！");	
				}
				Log.info("[Notification:"+ notificationName+"]");
				controllerMap[controllerName][0].handleNotification(new Notification(notificationName,body,type));
				return;
			}
			
			for each(var key:Object in controllerMap)
			{
				var commandArray:Array = key[1] as Array;
				if(commandArray.length <= 0) continue;
				if(commandArray.indexOf(notificationName) != -1)
				{
					Log.info("[Notification:"+ notificationName+"]");
					key[0].handleNotification(new Notification(notificationName,body,type));
					
				}
			}
			
			
			for each(var key2:Object in asyncUtilsMap)
			{
				var item:AsyncUtils = key2 as AsyncUtils;
				if(item)
				{
					var obj:Object = item.getCurrentMethodObj();
					if(!StringUtils.isEmpty(obj.resultNotificationName))
					{
						if(obj.resultNotificationName == notificationName)
						{
							item.__onResult();
						}
					}
				}
			}
		}
		/**
		 * 注册异步调用方法集
		 * @param name
		 * @param async
		 * 
		 */		
		public function registerAsync(name:String,async:AsyncUtils):void
		{
			asyncUtilsMap[name] = async;
			
		}
		public function removeAsync(name:String):void
		{
			asyncUtilsMap[name] = null;
			delete asyncUtilsMap[name];
		}
		private var controllerMap:Dictionary;
		private var asyncUtilsMap:Dictionary;
	}
}